// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    today: "",
    day: 1,
    functionData: [
      '../../images/ai.png',
      '../../images/zp.png',
      '../../images/ai2.png',
      '../../images/sc.png',
    ],
    ainame: '',
    mNEData: [
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7SQiC.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7Selt.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7SKD5.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7S2Le.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7YLV6.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SObX.jpg',
          star: '2',
          isFinish: false,
        }
      ],
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7S7w5.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SlER.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7SCor.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7Sz0k.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7SZoD.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7YPFT.jpg',
          star: '2',
          isFinish: false,
        }
      ],
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7S6hz.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7S8nu.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7S3Uy.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SyFt.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7YGDp.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7YW7Q.jpg',
          star: '2',
          isFinish: false,
        }
      ],
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7SAtG.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SbcD.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7Sj6C.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7S9yR.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7YnNh.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7Y52S.jpg',
          star: '2',
          isFinish: false,
        }
      ],
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7SXz4.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SUIX.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7Sa5z.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SiNu.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7YQPW.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7Ydb2.jpg',
          star: '2',
          isFinish: false,
        }
      ],
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7Suwp.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7S1x6.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7Sx2G.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7SML4.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7Ye4H.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7YlML.jpg',
          star: '2',
          isFinish: false,
        }
      ],
      [{
          title: '早餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7rSgW.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7rNHS.jpg',
          star: '2.5',
          isFinish: false,
        },
        {
          title: '午餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7YAyO.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7Y8kU.jpg',
          star: '3',
          isFinish: false,
        },
        {
          title: '晚餐',
          one: 'https://s3.jpg.cm/2021/07/10/I7YUS8.jpg',
          two: 'https://s3.jpg.cm/2021/07/10/I7Yc7w.jpg',
          star: '2',
          isFinish: false,
        }
      ]
    ]
  },
  dk: function (e) {
    var u = wx.getStorageSync('user')
    this.setData({
      userinfo: u.userxx
    })
    if (this.data.userinfo == undefined || this.data.userinfo == '') {
      wx.navigateTo({
        url: '../denglu/denglu?type=index',
      })
      return
    }
    console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    var d = this.data.mNEData
    console.log(d);
    d[this.data.day][i].isFinish = true
    this.setData({
      mNEData: d
    })
    
    this.saveFakeData()

    var itk = 0
    if (i == 0 || i == 2) {
      itk = wx.getStorageSync('intake')
      itk += 500
      console.log(itk);
    } else {
      itk = wx.getStorageSync('intake')
      itk += 1000
      console.log(itk);
    }
    wx.setStorageSync('intake', itk)
  },
  functionTap: function (e) {
    var u = wx.getStorageSync('user')
    this.setData({
      userinfo: u.userxx
    })
    if (this.data.userinfo == undefined || this.data.userinfo == '') {
      wx.navigateTo({
        url: '../denglu/denglu?type=index',
      })
      return
    }
    console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    if (i == 0) {
      this.AI_recognition_click()
    } else if (i == 1) {
      wx.navigateTo({
        url: '../zhuanpan/zhuanpan',
      })
    } else if (i == 2) {
      //问答机器人
      wx.navigateTo({
        url: '../robot/robot',
      })

    } else if (i == 3) {
      wx.navigateTo({
        url: '../collection/collection',
      })
    }
  },
  goToSearch: function () {
    wx.navigateTo({
      url: '../sousuo/sousuo',
    })
  },
  dispath: function (e) {
    //e.currentTarget.dataset.text获取点击的模块的值
    console.log(e);
    wx.navigateTo({
      url: '../dishDetails/dishDetails?info=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.index)),
    })
    console.log(e.currentTarget.dataset.index);
  },
  //调取相机和相册
  AI_recognition_click: function () {
    var FSM = wx.getFileSystemManager();
    var ainame;
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: '正在识别...',
        })
        const tempFilePaths = res.tempFilePaths
        for (let r in res.tempFilePaths) {
          console.log(res.tempFilePaths[r]);
          FSM.readFile({
            filePath: res.tempFilePaths[r],
            encoding: "base64",
            success: function (data) {
              wx.request({
                url: 'https://www.daizhongdai.xyz:3007/getApple',
                method: 'POST',
                data: {
                  image: data.data
                },
                success: (res) => {
                  console.log(res);
                  if (res.data.error_code == 216202) {
                    wx.showModal({
                      title: '图片太大了,换一张吧'
                    })
                    return
                  }
                  var n = res.data.result[0].name
                  var r = res.data.result
                  console.log("cainame" + n);
                  if (n == "非果蔬食材") {
                    // console.log(data.data);
                    wx.request({
                      url: 'https://www.daizhongdai.xyz:3007/getDish',
                      method: 'POST',
                      headers: {
                        "content-type": "application/x-www-form-urlencoded"
                      },
                      data: {
                        image: data.data
                      },
                      success: res => {
                        console.log(res);
                        if (res.data.error_code == 216202) {
                          wx.showModal({
                            title: '图片太大了,换一张吧'
                          })
                          return
                        }
                        n = res.data.result[0].name
                        r = res.data.result
                        wx.navigateTo({
                          url: '../nutrition/nutrition?name=' + JSON.stringify(r)
                        })
                      },
                      fail: (err) => {
                        console.log(err);
                        wx.showToast({
                          title: '识别失败，请检查网络！',
                        })
                      },
                      complete: () => {
                        wx.hideLoading({
                          success: (res) => {},
                        })
                      }
                    })
                    return
                  }
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  wx.navigateTo({
                    url: '../nutrition/nutrition?name=' + JSON.stringify(r)
                  })
                },
                fail: () => {
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  wx.showToast({
                    title: '识别失败，请检查网络！',
                  })
                }
              })
            }
          })
          break
        }
      }
    })


  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  add0: function (d) {
    return d < 10 ? '0' + d : d
  },

  onLoad() {
 
    // var n = require('../../utils/n')
    // console.log(111111);
    // n.get('/login?code=063kuR000Oic1M1KyQ200gli9U1kuR0A')
    // .then(res=>{
    //   console.log(res);
    // }).catch(err=>{
    //   console.log(err);
    // })

    var d = new Date()
    this.setData({
      today: d.getFullYear() + "年" + this.add0(d.getMonth() + 1) + "月" + this.add0(d.getDate()) + "日",
      day: d.getDay(),
    })
    // console.log(this.data.day);
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

  },
  saveFakeData(){
    var zzw = wx.getStorageSync('zzw')
    if (zzw == undefined || zzw == "") {
      //初始化
      zzw = {
        count: this.data.day,
        data: this.data.mNEData
      }
      wx.setStorageSync('zzw', zzw)
      return
    }

    zzw.count = this.data.day
    zzw.data = this.data.mNEData

    if (zzw.count  == 6 ) {
      //重置
      console.log('重置重置重置重置重置重置');
      var d = this.data.mNEData
      for (const key in d) {
        if(key==6)
        continue
        for (const key2 in d[key]) {
          d[key][key2].isFinish = false
        }
      }
      
      console.log(d);
      zzw.data = d
    }
    console.log('存存存存存存存存存存存');
    wx.setStorageSync('zzw', zzw)
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShow() {
    var zzw = wx.getStorageSync('zzw')
    console.log("zzw"+zzw);

      console.log('取取取取取取取取取取取取');
      this.setData({
        mNEData:zzw.data==undefined?this.data.mNEData:zzw.data
      })
  },
  onHide() {

  
  },
  onUnload() {
this.saveFakeData()
  },
})